package com.open.pokergames.comm.repository.viewModel;

import com.open.pokergames.NetConfig
import com.open.pokergames.comm.repository.Api
import com.open.pokergames.comm.repository.entity.*
import com.sinothk.rxretrofit.RxRetrofit
import com.sinothk.rxretrofit.param.RetrofitParam
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.io.File


class SystemViewModel {

    fun login(loginVo: LoginVo) {

        RxRetrofit.init(NetConfig.BASE_URL)//, ServerConfig.getHeader()
            .create(Api::class.java)
            .login(loginVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<LoginDto>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<ResultInfo<LoginDto>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "login"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<LoginDto>) {
                    result.eventType = "login"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun register(userVo: UserVo) {
        RxRetrofit.init(NetConfig.BASE_URL)//, ServerConfig.getHeader()
            .create(Api::class.java)
            .register(userVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<LoginDto>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<ResultInfo<LoginDto>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "register"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<LoginDto>) {
                    result.eventType = "register"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun loginAuto() {
        RxRetrofit.init(NetConfig.BASE_URL, NetConfig.getHeader())//
            .create(Api::class.java)
            .loginAuto()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<LoginDto>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<ResultInfo<LoginDto>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "loginAuto"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<LoginDto>) {
                    result.eventType = "loginAuto"
                    EventBus.getDefault().post(result)
                }
            })
    }

    // 版本检测
    fun lastVersion(flag: String) {
        RxRetrofit.init(NetConfig.BASE_URL) //, ServerConfig.getHeader()
            .create(Api::class.java)
            .lastVersion()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppVersionEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val resultInfo = ResultInfo<AppVersionEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message));
                    resultInfo.eventType = flag
                    EventBus.getDefault().post(resultInfo)
                }

                override fun onNext(resultInfo: ResultInfo<AppVersionEntity>) {
                    resultInfo.eventType = flag
                    EventBus.getDefault().post(resultInfo)
                }
            })
    }

    fun uploadFile(bizType: String, filePath: String) {
        RxRetrofit.init(NetConfig.BASE_URL, NetConfig.getHeader())
            .create(Api::class.java)
            .uploadFile(bizType, RetrofitParam.createFileParam("file", File(filePath)))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<FileEntity>>() {

                override fun onCompleted() {
                }

                override fun onError(e: Throwable) {
                    val result = ResultInfo<FileEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "uploadFile"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<FileEntity>) {
                    result.eventType = "uploadFile"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun editUser(userVo: UserVo) {
        RxRetrofit.init(NetConfig.BASE_URL, NetConfig.getHeader())//, ServerConfig.getHeader()
            .create(Api::class.java)
            .editUser(userVo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<UserEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<ResultInfo<UserEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "editUser"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<UserEntity>) {
                    result.eventType = "editUser"
                    EventBus.getDefault().post(result)
                }
            })
    }
}
